#! /bin/bash 

# 定义一个红色显示的函数
function echo_red () {
    local what=$*
    echo -e "\e[1;31m${what}\e[0m"
}

# 定义一个绿色显示的函数
function echo_green () {
    local what=$*
    echo -e "\e[1;32m${what}\e[0m"
}

cpu_used=`vmstat | awk 'NR==3{print (100-$(NF-2))}'`
cpu_average=`uptime | awk '{print $(NF-2),$(NF-1),$NF}'`
mem_used=`free -h |awk 'NR==2{print int(100-($NF/$2*100))}'`
disk_used=`df -hT |grep /$ | awk '{print $(NF-1)*100/100}'`

echo "CPU利用率：$cpu_used%"
echo "CPU负载: $cpu_average"
echo "内存利用率: $mem_used%"
echo "根目录利用率：$disk_used%"

echo "==========检查结果=========="

if [ $cpu_used -ge 60 ]
then
   echo_red "cpu利用率过大于60%，请检查"

elif [ $mem_used -ge 80 ]
then
   echo_red "内存利用率过大于60%，请检查"

elif [ $disk_used -ge 60 ]
then
   echo_red "磁盘利用率过大于60%，请检查"

else
   echo_green "结果正常"
fi
